﻿using Lamex.Base.Framework.BaseDal;
using Lamex.SystemManager.Interfaces;
using Lamex.SystemManager.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.SystemManager.DalServer
{
    public class tb_OrgStucture : BaseDalSQL<tb_OrgStuctureInfo>, Itb_OrgStucture
    {
        /// <summary>
        /// 根据指定机构节点ID，获取其下面所有机构列表
        /// </summary>
        /// <param name="parentId">指定机构节点ID</param>
        /// <returns></returns>
        public List<tb_OrgStuctureInfo> GetAllOUsByParent(int parentId)
        {
            List<tb_OrgStuctureInfo> list = new List<tb_OrgStuctureInfo>();
            string sql = string.Format("Select * From {0} Order By PID, Name ", tableName);

            DataTable dt = GetDataTableBySql(sql);
            string sort = string.Format("{0} {1}", "PID", isDescending ? "DESC" : "ASC");
            DataRow[] dataRows = dt.Select(string.Format(" PID = {0}", parentId), sort);
            for (int i = 0; i < dataRows.Length; i++)
            {
                string id = dataRows[i]["ID"].ToString();
                list.AddRange(GetOU(id, dt));
            }

            return list;
        }
        private bool isDescending = false;//是否以降序排列
        private List<tb_OrgStuctureInfo> GetOU(string id, DataTable dt)
        {
            List<tb_OrgStuctureInfo> list = new List<tb_OrgStuctureInfo>();

            tb_OrgStuctureInfo ouInfo = GetEntriyList(" 1=1 and  ID='" + id + "'").FirstOrDefault();
            list.Add(ouInfo);

            string sort = string.Format("{0} {1}", "PID", isDescending ? "DESC" : "ASC");
            DataRow[] dChildRows = dt.Select(string.Format(" PID={0} ", id), sort);
            for (int i = 0; i < dChildRows.Length; i++)
            {
                string childId = dChildRows[i]["ID"].ToString();
                List<tb_OrgStuctureInfo> childList = GetOU(childId, dt);
                list.AddRange(childList);
            }
            return list;
        }

        /// <summary>
        /// 获取指定机构下面的树形列表
        /// </summary>
        /// <param name="mainOUID">指定机构ID</param>
        public List<OUNodeInfo> GetTreeByID(int mainOUID)
        {
            List<OUNodeInfo> arrReturn = new List<OUNodeInfo>();
            string sql = string.Format("Select * From {0} Order By PID, Name ", tableName);

            DataTable dt = GetDataTableBySql(sql);
            string sort = string.Format("{0} {1}","PID", isDescending ? "DESC" : "ASC");
            DataRow[] dataRows = dt.Select(string.Format(" PID = {0}", mainOUID), sort);
            for (int i = 0; i < dataRows.Length; i++)
            {
                string id = dataRows[i]["ID"].ToString();
                OUNodeInfo menuNodeInfo = GetNode(id, dt);
                arrReturn.Add(menuNodeInfo);
            }

            return arrReturn;
        }
        private OUNodeInfo GetNode(string id, DataTable dt)
        {
            tb_OrgStuctureInfo ouInfo = this.GetEntriyList(" 1=1 and ID='" + id + "'").FirstOrDefault();
            OUNodeInfo ouNodeInfo = new OUNodeInfo(ouInfo);

            string sort = string.Format("{0} {1}", "PID", isDescending ? "DESC" : "ASC");
            DataRow[] dChildRows = dt.Select(string.Format(" PID={0} ", id), sort);

            for (int i = 0; i < dChildRows.Length; i++)
            {
                string childId = dChildRows[i]["ID"].ToString();
                OUNodeInfo childNodeInfo = GetNode(childId, dt);
                ouNodeInfo.Children.Add(childNodeInfo);
            }
            return ouNodeInfo;
        }



        /// <summary>
        /// 获取集团和公司的列表
        /// </summary>
        /// <returns></returns>
        public List<tb_OrgStuctureInfo> GetCompany()
        {
            string StrWHere = string.Format(" Category='公司' or Category='集团' ");
            return GetEntriyList(StrWHere);
        }

        public List<tb_OrgStuctureInfo> GetAllCompany()
        {
            string StrWHere = string.Format("Category='公司' ");
            return GetEntriyList(StrWHere);
        }
    }
}
